<?php

namespace app\modules\backend\controllers;

use app\models\Base;
use app\models\Cate;
use app\tools\Re;
use Yii;
use yii\helpers\Json;

class CateController extends BaseController
{
    /**
     * @param string $type
     * @return array
     */
    public function actionCateList(){
        $pid = Yii::$app->request->post('pid', 0);
        $page = Yii::$app->request->post('page', 0);
        $name = Yii::$app->request->post('name', 0);
        $cate_query = Cate::find()->where(['pid' => $pid, 'status' => [0,1]]);
        if ($name) {
            $cate_query->andWhere(['like', 'name', $name]);
        }
        $cate = $cate_query->orderBy("sort desc, created_at desc")->limit(Cate::PAGE_SIZE)->offset(Cate::PAGE_SIZE*$page)->all();
        $cate_data = [];
        if ($cate) {
            foreach ($cate as $v) {
                $cate_data[] = [
                    'id' => $v->id,
                    'pid' => $v->pid,
                    'name' => $v->name,
                    'sub_name' => $v->sub_name,
                    'logo' => $v->logo,
                    'cover' => $v->cover,
                    'sort' => $v->sort,
                    'status' => $v->status,
                    'created_time' => date("Y-m-d H:i", $v->created_at),
                ];
            }
        }
        return Re::data([
            'data' => $cate_data,
            'totalPage' => $cate_query->count(),
            'currentPage' => $page,
            'perPage' => Cate::PAGE_SIZE
        ]);
    }

    public function actionEditCate(){
        $id = Yii::$app->request->post('id', 0);
        $post = Yii::$app->request->post();
        return $this->edit(Cate::class, $id, $post);
    }

    public function actionChangeCateStatus(){
        $id = Yii::$app->request->post('id', 0);
        $status = Yii::$app->request->post('status', 0);
        $cate = Cate::findOne($id);
        if (empty($cate)) {
            return Re::data(null, '品类信息读取失败', Re::ERROR);
        }
        $cate->status =  $status;
        if (!$cate->save()) {
            return Re::data(null, '操作失败', Re::ERROR);
        }
        return Re::data();
    }

    public function actionListForSelect(){
        $cate_data = [];
        $cate_one = Cate::find()->where(['pid' => 0])->orderBy("created_at desc")->all();
        if (!empty($cate_one)) {
            foreach ($cate_one as $v1) {
                $cate_two_data = [];
                $cate_two = Cate::find()->where(['pid' => $v1->id])->orderBy("created_at desc")->all();
                if (!empty($cate_two)) {
                    foreach ($cate_two as $v2) {
                        $cate_two_data[] = [
                            'value' => (int)$v2->id,
                            'label' => $v2->name,
                        ];
                    }
                } else {
                    continue;
                }
                $cate_data[] = [
                    'value' => (int)$v1->id,
                    'label' => $v1->name,
                    'children' => $cate_two_data
                ];
            }
        }
        return Re::data(['data' => $cate_data]);
    }
}
